package demo3.boot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class ScheduledService {
    @Autowired
    JavaMailSender mailSender;
    //秒   分   时     日   月   周几
    //0 * * * * MON-FRI
    //注意cron表达式的用法；0/10 * * * * ?   表示每10秒 执行任务
    @Scheduled(cron = "0/10 * * * * ?")
    public void abc(){
        System.out.println("周期定时器执行。。。");
    }

    @Scheduled(cron = "0/10 * * * * ?")
    public void sendMail() throws MessagingException {
        System.out.println("10秒发一次邮件。。。");
        //        邮件设置：一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setSubject("主题");
        mimeMessageHelper.setText("<b style='color:blue'>今天 7:30来开会</b>",true);
        //发送附件
        mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Desktop\\vue\\build\\logo.png"));
        mimeMessageHelper.addAttachment("2.jpg",new File("C:\\Users\\Administrator\\Desktop\\apache-tomcat-9.0.31\\webapps\\host-manager\\images\\tomcat.gif"));
        mimeMessageHelper.setTo("1145085640@qq.com");
        mimeMessageHelper.setFrom("1002763351@qq.com");
        mailSender.send(mimeMessage);
    }
}
